package com.example.yls.demoa;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;

import java.io.File;
import java.util.List;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;
import cn.bmob.v3.listener.UploadFileListener;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private Button btn_reg;
    private Button btn_log;
    private Button btn_info;
    private Button btn_add;
    private Button btn_query;
    private Button btn_delete;
    private Button btn_recyclerview;
    private ImageView headimg_iv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn_log = findViewById(R.id.btn_log);
        btn_reg = findViewById(R.id.btn_reg);
        btn_info = findViewById(R.id.btn_info);
        btn_add = findViewById(R.id.btn_add);
        btn_query = findViewById(R.id.btn_query);
        btn_delete = findViewById(R.id.btn_delete);
        btn_recyclerview = findViewById(R.id.btn_recyclerview);
        headimg_iv = findViewById(R.id.headimg_iv);

        btn_recyclerview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,StudentListActivity.class);
                startActivity(intent);

            }
        });

        btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BmobQuery<Student> studentBmobQuery = new BmobQuery<>();
                studentBmobQuery.addWhereEqualTo("profession", "软件专业");
//                studentBmobQuery.addWhereEqualTo("age","18");

                studentBmobQuery.findObjects(new FindListener<Student>() {
                    @Override
                    public void done(List<Student> list, BmobException e) {
                        if (e == null) {
                            for (Student student : list) {
                                student.delete(new UpdateListener() {
                                    @Override
                                    public void done(BmobException e) {
                                        if (e == null) {
                                            toast("删除成功");
                                        } else {
                                            toast("删除失败" + e.getMessage());
                                        }

                                    }
                                });

                            }

                        }
                    }
                });

            }
        });


        btn_query.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BmobQuery<Student> studentBmobQuery = new BmobQuery<>();
//                studentBmobQuery.addWhereEqualTo("profession", "软件专业");
//                studentBmobQuery.addWhereEqualTo("age","20");
                studentBmobQuery.addWhereEqualTo("name","悟饭");

                studentBmobQuery.findObjects(new FindListener<Student>() {
                    @Override
                    public void done(List<Student> list, BmobException e) {
                        if (e == null) {
                          StringBuffer stringBuffer = new StringBuffer();
                          for (Student student:list){
                              stringBuffer.append(student).append("\n");
                              BmobFile headimg= student.getHeadimg();
                              Glide.with(MainActivity.this).load(headimg.getUrl()).into(headimg_iv);
                          }
                          toast(stringBuffer.toString());



                        }
                    }
                });


            }
        });


        btn_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BmobUser bmobUser = new BmobUser();
                bmobUser.setUsername("阿迪");
                bmobUser.setPassword("123456");
                bmobUser.signUp(new SaveListener<BmobUser>() {
                    @Override
                    public void done(BmobUser bmobUser, BmobException e) {
                        if (e == null) {
                            toast("注册成功" + bmobUser.getUsername());
                        } else {
                            toast("注册失败" + e.getMessage());
                        }
                    }
                });
            }
        });


        btn_log.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BmobUser bmobUser = new BmobUser();
                bmobUser.setUsername("阿迪");
                bmobUser.setPassword("123456");
                bmobUser.login(new SaveListener<BmobUser>() {
                    @Override
                    public void done(BmobUser bmobUser, BmobException e) {
                        if (e == null) {
                            toast("登录成功" + bmobUser.getUsername());
                        } else {
                            toast("登录失败" + e.getMessage());
                        }
                    }
                });

            }
        });

        btn_info.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MyUser myUser = BmobUser.getCurrentUser(MyUser.class);
                myUser.setSex("男");
                myUser.setAge(18);
                myUser.update(new UpdateListener() {
                    @Override
                    public void done(BmobException e) {
                        if (e == null) {
                            toast("更新成功");
                        } else {
                            toast("更新失败" + e.getMessage());
                        }
                    }
                });

            }
        });

        btn_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String imgPath = "/mnt/shared/Image/Koala.jpg";
                final BmobFile headimg = new BmobFile(new File(imgPath));
                headimg.upload(new UploadFileListener() {
                    @Override
                    public void done(BmobException e) {
                        if (e == null) {
                            Log.e("Bmob", "文件上传失败");
                            Student student = new Student();
                            student.setName("悟饭");
                            student.setAge(20);
                            student.setProfession("软件专业");
                            student.setScore(90);
                            student.setHeadimg(headimg);
                            student.save(new SaveListener<String>() {
                                @Override
                                public void done(String s, BmobException e) {
                                    if (e == null) {
                                        toast("保存成功" + s);
                                    } else {
                                        toast("保存失败" + e.getMessage());
                                    }
                                }
                            });


                        } else {
                            Log.e("Bmob", "文件上传失败");
                        }

                    }
                });

            }
        });


    }

    private void toast(String s) {
        Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
    }
}
